import { setlocalStorageItem, getlocalStorageItem, removelocalStorageItem } from './../../utils/auth'
// initial state
const state = () => ({
  userInfo: getlocalStorageItem('userInfo') ? getlocalStorageItem('userInfo') : {},
  testData: [
    {
      'id': '111',
      'policyName': '测01',
      'policyContent': '测01测01测01',
      'policyType': null,
      'policyKeyword': null,
      'policyCollection': null,
      'policyBrowse': null,
      'status': null,
      'createTime': null,
      'createName': null,
      'updateTime': null,
      'updateName': null
    }
  ]
})

// actions
const actions = {
  setUserInfo({ commit }, data) { // 保存用户信息
    commit('SET_USER_INFO', data)
  },

  removeUserInfo({ commit }) { // 退出清空用户所有数据
    commit('REMOVE_USER_INFO')
  },

  updateAvatar({ commit }, data) {
    console.log(data)
    commit('UPDATE_AVATAR', data)
  }
}

// mutations
const mutations = {
  SET_USER_INFO(state, data) {
    state.userInfo = { ...state.userInfo, ...data }

    setlocalStorageItem('userInfo', JSON.stringify(data))
  },

  REMOVE_USER_INFO(state) {
    state.userInfo = {}
    removelocalStorageItem()
  },

  UPDATE_AVATAR(state, data) {
    state.userInfo.userPicture = data
    setlocalStorageItem('userInfo', JSON.stringify(state.userInfo))
  }

}

export default {
  namespaced: true,
  state,
  actions,
  mutations
}
